// <view class='canvas-box' style='width:{{cOpt.width}}px;height:{{cOpt.height}}px;'>
//    <canvas canvas-id='{{cOpt.id}}' style='width:{{cOpt.width}}px;height:{{cOpt.height}}px;'></canvas>
// </view>

module.exports = {

   /**
    * @2018.11.08 create
    * 绘制背景
    * @canvasOpt {Object} 画布属性
    * @color {String} 背景颜色
    * @x {Number} x坐标
    * @y {Number} y坐标
    * @that {Object} 页面对象
    */
   drawCanvasBgColor(canvasOpt, color, x, y, that) {
      let ctx = that.data.ctx;
      ctx.setFillStyle(color);
      ctx.fillRect(0, 0, x, y);
   },

   drawCanvasBgImage(canvasOpt, src, x, y, width, height, that) {
      let ctx = that.data.ctx;
      ctx.drawImage(src, x, y, width, height);
   },

   /**
    * @2018.11.08 create
    * 绘制文本
    * @canvasOpt {Object} 画布属性
    * @text {String} 文本
    * @x {Number} x坐标
    * @y {Number} y坐标
    * @align {String} 对齐方式
    * @that {Object} 页面对象
    */
   drawText(canvasOpt, text, x, y, color, size, align, that) {
      let ctx = that.data.ctx;
      ctx.fillStyle = color;
      ctx.setFontSize(size);
      ctx.setTextAlign(align);
      ctx.fillText(text, x, y);
   },

   /**
    * @2018.11.08 create
    * 绘制多行文本
    */
   drawLongText(canvasOpt, text, x, y, color, size, align, that) {
      let ctx = that.data.ctx;
      ctx.setFillStyle(color)
      ctx.fillStyle = color;
      ctx.font = `normal normal ${size}px 微软雅黑`;
      ctx.setFontSize(size)
      let metrics = ctx.measureText('标'); //标尺，一个字的宽度
      // 正文 单行显示字符数，左右各预留两个字位置-8；
      let contentRowLen = Math.trunc((that.data.systemInfo.windowWidth - x) / metrics.width) * 2 - 8;
      console.log(contentRowLen)
      let [contentLeng, contentArray, contentRows] = this.textByteLength(text, contentRowLen);
      ctx.setTextAlign(align);
      let contentHh = size * 1.5; //每行的高度
      console.log(contentArray)
      for (let m = 0; m < contentArray.length; m++) {
         ctx.fillText(contentArray[m], x - 10, y + contentHh * m); // lfet/2左边距  top上边距
      }
   },

   /**
    * @2018.11.08 create
    * @text {String} 传入的文本
    * @num {Number} 单行显示的字节长度
    */
   textByteLength(text, num) {
      let strLength = 0; // text byte length
      let rows = 1;
      let str = 0;
      let arr = [];
      for (let j = 0; j < text.length; j++) {
         if (text.charCodeAt(j) > 255) {
            strLength += 2;
            if (strLength > rows * num) {
               strLength++;
               if (rows % 2 == 0) {
                  arr.push(text.slice(str, j + 1));
               } else {
                  arr.push(text.slice(str, j));
               }
               str = j;
               rows++;
            }
         } else {
            strLength++;
            if (strLength > rows * num) {
               arr.push(text.slice(str, j));
               str = j;
               rows++;
            }
         }
      }
      arr.push(text.slice(str, text.length));
      return [strLength, arr, rows] //  [处理文字的总字节长度，每行显示内容的数组，行数]
   },

   /**
    * @2018.11.08 create
    * 绘制图片
    */
   drawImage(canvasOpt, src, x, y, width, height, that) {
      let ctx = that.data.ctx;
      ctx.drawImage(src, x, y, width, height);
   },

   /**
    * @2018.11.08 create
    * 绘制头像
    * @canvasOpt {Object} 画布属性
    * @src {String} 图片资源
    * @arcx {Number} 圆形x坐标
    * @arcy {Number} 圆形y坐标
    * @r {Number} 圆形半径
    * @px {Number} 图片x坐标
    * @py {Number} 图片y坐标
    * @width {Number} 图片宽
    * @height {Number} 图片高
    * drawCanvas.drawAvatar(that.data.cOpt, 60, 335, 36, downFill.codeimg, 25, 301, 68, 68, that);
    */
   drawAvatar(canvasOpt, arcx, arcy, r, src, px, py, width, height, that) {
      let ctx = that.data.ctx;
      ctx.save();
      ctx.beginPath();
      ctx.arc(arcx, arcy, r, 0, 2 * Math.PI);
      ctx.setFillStyle('#ffffff');
      ctx.fill();
      ctx.clip();
      ctx.drawImage(src, px, py, width, height);
      ctx.restore();
   },

   /**
    * @2018.11.08 create
    * 绘制头像+二维码
    * @canvasOpt {Object} 画布属性
    * @src {String} 图片资源
    * @arcx {Number} 圆形x坐标
    * @arcy {Number} 圆形y坐标
    * @r {Number} 圆形半径
    * @px {Number} 图片x坐标
    * @py {Number} 图片y坐标
    * @width {Number} 图片宽
    * @height {Number} 图片高
    * drawCanvas.drawAvatarAndCode(that.data.cOpt, 60, 330, 34, downFill.avatarUrl, 25, 296, 68, 68, 250, 330, 36, downFill.codeimg, 216, 296, 68, 68, that);
    */
   drawAvatarAndCode(canvasOpt, acx, acy, ar, src, ax, ay, awidth, aheight, bcx, bcy, br, code, bx, by, bwidth, bheight, that) {
      let ctx = that.data.ctx;
      ctx.save();
      ctx.beginPath();
      ctx.arc(acx, acy, ar, 0, 2 * Math.PI);
      ctx.arc(bcx, bcy, br, 0, 2 * Math.PI);
      ctx.setFillStyle('#ffffff');
      ctx.fill();
      ctx.clip();
      ctx.drawImage(src, ax, ay, awidth, aheight);
      ctx.drawImage(code, bx, by, bwidth, bheight);
      ctx.restore();
   },

   /**
    * @2018.11.08 create
    * canvas导出图片
    * @canvasOpt {Number} 画布属性
    * @callback {Function} callback 回调函数
    * @failback {Function} failback 回调函数
    */
   canvasExportImage(canvasOpt, callback, failback) {
      let that = this;
      wx.canvasToTempFilePath({
         x: 0,
         y: 0,
         width: canvasOpt.width * 2,
         height: canvasOpt.height * 2,
         canvasId: canvasOpt.id,
         success(res) {
            callback(res.tempFilePath);
         },
         fail(err) {
            console.log('画布导出资源错误', err)
            failback(err);
            wx.showModal({
               title: "提示",
               content: '海报资源生成错误，请稍后重试',
               showCancel: false,
               confirmText: '知道了',
            })
         }
      })
   },

   /**
    * 打开设置页面后的回调
    */
   bindopensettingSaveImage(res) {
      let that = this;
      console.log('打开设置页面回调')
      wx.showLoading({
         title: '请稍后',
         mask: true,
      })
      that.authorizeSaveImageCheck();
   },


   /**
    * @2018.11.08 create
    * @授权
    * 保存图片到相册授权
    * @scope {String} 授权项
    * @callback {Function} 成功回调
    * @failback {Function} 失败回调
    */
   authorizeCheck(scope, callback, failback) {
      wx.authorize({
         scope: "scope." + scope,
         success: function(res) {
            callback();
         },
         fail: function(res) {
            console.log('授权拒绝')
            failback();
         },
      })
   },

   /**
    * @2018.11.08 create
    * 保存图片到用户本地
    * @path {String} 保存资源临时路径
    */
   saveImageToPhotosAlbum(path, callback, failback) {
      let that = this;
      wx.saveImageToPhotosAlbum({
         filePath: path,
         success(res) {
            wx.showModal({
               title: "保存成功",
               content: '专属海报已保存至手机相册，快去发朋友圈吧',
               showCancel: false,
               confirmText: '知道了',
            })
            callback();
         },
         fail: function(res) {
            failback();
         }
      })
   },
}